<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form" ref="ruleForm">
        <p-row>
          <p-col>
            <p-form-item
              label="产品类别"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <p-select
                v-decorator="[
                  'productTypeName',
                  validatorRules.productTypeName,
                ]"
                placeholder="请输入产品类别"
                @change="changeSelect"
              >
                <p-select-option
                  v-for="item in datalist"
                  :key="item"
                  :value="item"
                  >{{ item }}</p-select-option
                >
              </p-select>
            </p-form-item>
          </p-col>
          <p-col>
            <p-form-item
              label="产品编码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <p-select
                v-decorator="['productCode', validatorRules.productCode]"
                placeholder="请选择产品编码"
                @change="changInfo"
              >
                <p-select-option
                  v-for="item in dataInfo"
                  :key="item.mcode"
                  :value="item.mcode"
                  >{{ item.mcode }}</p-select-option
                >
              </p-select>
            </p-form-item>
          </p-col>
          <p-col>
            <p-form-item
              label="产品名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <p-input
                readOnly
                v-decorator="['productName', validatorRules.productName]"
                placeholder="请输入产品名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col>
            <p-form-item
              label="老化时长(分钟)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <p-input
                v-decorator="['agingTime', validatorRules.agingTime]"
                placeholder="请输入老化时长"
                :min="1"
              />
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction, getAction } from "@/api/manage";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";
import { validateDuplicateValue } from "@/utils/util";
export default {
  name: "",
  mixins: [SubmitOnchangMixin],
  components: {},
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      datalist: [],
      dataInfo: [],
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      wrapperCol1: {
        xs: { span: 24 },
        sm: { span: 19 },
      },
      confirmLoading: false,
      url: {
        add: "/mes-manufacture/mfAgingRules", //新增老化规则
        edit: "/mes-manufacture/mfAgingRules/updateAgingRules", //编辑老化规则
        datalist: "/basic-business/auto/baCpinfoMain/queryMtypeList", //字典查询
        dataInfo: "/basic-business/auto/baCpinfoMain/queryListByMtypeno", //产品选择
      },
      treeData: [],
      validatorRules: {
        productTypeName: {
          rules: [
            {
              required: true,
              message: "请选择产品类别!",
            },
          ],
        },
        productCode: {
          rules: [{ required: true, message: "请选择产品编码!" }],
        },
        productName: {
          rules: [{ required: true, message: "请输入产品名称!" }],
        },
        agingTime: {
          rules: [
            {
              required: true,
              message: "请输入老化时长",
            },
            {
              pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,
              message: "请输入有效时间",
            },
          ],
        },
      },
    };
  },
  mounted() {
    // this.loadData()
  },
  methods: {
    add() {
      this.edit({});
      this.getRuleCodeValue();
    },
    // 下拉框数据
    getRuleCodeValue() {
      getAction(this.url.datalist, {}).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          const item = res.data;
          this.datalist = item;
        }
      });
    },
    changeSelect(val) {
      this.form.resetFields();
      getAction(this.url.dataInfo, { mtypeno: val }).then((res) => {
        if (res.success) {
          this.dataInfo = res.data;
        }
      });
    },
    changInfo(val) {
      let item = this.dataInfo.filter((item) => item.mcode === val);
      this.form.setFieldsValue({
        productName: item[0].mname,
        productTypeCode: item[0].productTypeCode,
        sprc: item[0].sprc,
      });
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(this.model, [
            "productTypeName",
            "productTypeCode",
            "productCode",
            "productName",
            "agingTime",
            "sprc",
          ])
        );
      });
    },
    close() {
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);
          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
  },
};
</script>